﻿// registers the GMarker class
GMarker.registerClass('GMarker');

/*
    AAAMarker is a GMarker subclass that adds some useful features:
    - marker locking in a given point
    - integrated reverse geocoding by calling checkPosition method
    - various icon constants definition (at the end of file)
    - marker type managing (i.e. start marker, stop marker...)
    - info storing and showing on marker click
*/
MapControl2.AAAMarker = function(geoloc, locked, type, description)
{
    // invokes the base class constructor
    var args = new Array();
    var latlng = new google.maps.LatLng(parseFloat(geoloc.lat), parseFloat(geoloc.lng));
    args.push(latlng);
    
    this._normalImage = MapControl2.AAAMarker._iconArray[type];
    this._selectedImage = MapControl2.AAAMarker._selectedIconArray[type];
    this._currentImage = this._normalImage;
    this._dontshow = false;
    this._error = false;
    this._net = new MapControl2.AAANetUtils();
    this._isExploring = false;
    
    var myIcon = new google.maps.Icon(G_DEFAULT_ICON);
    myIcon.image = this._normalImage;
    
    if (type == MapControl2.AAAMarker.WAYPOINT_MARKER || type == MapControl2.AAAMarker.ROUTEHANDLER_MARKER )
    {
        myIcon.shadow = null;
        myIcon.iconSize = new GSize(11, 11);
        myIcon.iconAnchor = new GPoint(6, 6);
    }else if(type == MapControl2.AAAMarker.EXPLORATION_MARKER){
        myIcon.shadow = null;
        myIcon.iconSize = new GSize(30, 34);
        myIcon.iconAnchor = new GPoint(6, 6);
    }
    var opts = {draggable: !locked, icon: myIcon};
    args.push(opts);
    MapControl2.AAAMarker.initializeBase(this, args);
    
    // fills the fields
    this._geoloc = geoloc;      // geoloc associato
    this.setLocked(locked);      // flag di bloccaggio
    
    this._type = type;          // tipo del marker (start, end, waypoint....)
                                // per i vari tipi vedere la fine del file.
    this._tabs = new Array();   // descrizioni del marker
    this._isShowingInfoWindow = false; 
    this._mustBeFormatted = true;
    
    // se il marker è stato costruito specificando una descrizione si 
    // aggiunge un elemento all'array delle descrizioni.
    if (description)
    {
        var tab = new Object()
        tab.label = "Description";
        tab.content = description;
        this._tabs[0] = tab;
    }
    
    google.maps.Event.bind(this, 'click', this, this._markerClick);
    google.maps.Event.bind(this, 'dragstart', this, this._markerDragStart);
    google.maps.Event.bind(this, 'infowindowbeforeclose', this, this._infoWindowClose);
    
    if (!this._locked)
    {
        google.maps.Event.bind(this, 'dragend', this, this._markerDragEnd);
    }
    
    // passengers
    this._tookIn = false;
    this._tookOff = false;
};

// AAAMarker object definition
MapControl2.AAAMarker.prototype = {
   
    // -------------------------- event handlers -------------------------------------------------
    _markerDragEnd: function()
    {
        var myPos = this.getLatLng();
        this._geoloc.lat = myPos.lat();
        this._geoloc.lng = myPos.lng();
        if (this._error) this.resetError();
        
        this._geoloc.address = '...';
        this._mustBeFormatted = true;
        google.maps.Event.trigger(this, 'dragend2', this);
    },
    
    _markerClick: function()
    {
        if (this._dontshow || this._error) return;
        if (!this._isShowingInfoWindow)
        {
            this.showInfoWindow();
        }else{
            this.closeInfoWindow();
        }
    },
    
    _markerDragStart: function()
    {
        this._geoloc.address = 'reverse geocoding in progress...';
        if (this._isShowingInfoWindow) this.closeInfoWindow();
        if (this._error) this.resetError();
    },
    
    _infoWindowClose: function()
    {
        this._isShowingInfoWindow = false;
        this._currentImage = this._normalImage;
        
        for (i = 0; i<this._tabs.length; i++)
        {
           if (typeof this._tabs[i].destruct != 'undefined') this._tabs[i].destruct();
        }
    },
    // ----------------------------------------------------------------------------------------------
    
    // -------------------------- private function --------------------------------------------------
    // mostra una info window sul marker con le informazioni contenute nel campo descrizioni
    // La info window mostra una descrizione per tab.
    showInfoWindow: function()
    {
        if (this._dontshow) return;
        if (this._tabs.length > 0)
        {
            this._isShowingInfoWindow = true;
        
            var i = 0;
            var tabArray = new Array();
            for (i = 0; i<this._tabs.length; i++)
            {
                var tab = new google.maps.InfoWindowTab(this._tabs[i].label, this._tabs[i].content);
                tabArray.push(tab);
            }
            this.openInfoWindowTabsHtml(tabArray);
            
            
            for (i = 0; i<this._tabs.length; i++)
            {
                if (typeof this._tabs[i].initialize != 'undefined') this._tabs[i].initialize();
            }
            
            this._currentImage = this._selectedImage;
            MapControl2.AAAMarker.callBaseMethod(this, 'setImage', [this._currentImage]);
        }
    },
    
    closeInfoWindow: function()
    {
        MapControl2.AAAMarker.callBaseMethod(this, 'closeInfoWindow');
        this._isShowingInfoWindow = false;

        this._currentImage = this._normalImage;
        MapControl2.AAAMarker.callBaseMethod(this, 'setImage', [this._currentImage]);
    },
    // ---------------------------------------------------------------------------------
    
    // -------------------- public functions -------------------------------------------
    updateStatus: function(){
        var myPos = this.getLatLng();
        this._geoloc.lat = myPos.lat();
        this._geoloc.lng = myPos.lng();
    },
    
    _setImage: function(image, selected_img)
    {
        this._normalImage = image;
        this._selectedImage = selected_img;
        MapControl2.AAAMarker.callBaseMethod(this, 'setImage', [image]);
    },
    
    setImage: function(image, selected_img)
    {
        MapControl2.AAAMarker.callBaseMethod(this, 'setImage', [image]);
        this.redraw(true);
    },
    
    getImage: function()
    {
        return this._normalImage;
    },
    
    setError: function()
    {
        this._error = true;
        var image = MapControl2.AAAMarker.ERROR_MARKER;
        
        if (this._type == MapControl2.AAAMarker.WAYPOINT_MARKER)
            image = MapControl2.AAAMarker.WAYPOINT_ERROR_MARKER;
        MapControl2.AAAMarker.callBaseMethod(this, 'setImage', [image]);
    },
    
    resetError: function()
    {
        this._normalImage = MapControl2.AAAMarker._iconArray[this._type];
        this._selectedImage = MapControl2.AAAMarker._selectedIconArray[this._type];
        this._currentImage = this._normalImage;
        MapControl2.AAAMarker.callBaseMethod(this, 'setImage', [this._currentImage]);
        this._error = false;
    },
    
    checkPosition: function(checkCallback){
        this.disableDragging();
        var loadingImg = null, errImg = null, me = this, myImg = this._currentImage;
        
        if (this._type == MapControl2.AAAMarker.WAYPOINT_MARKER){
            loadingImg = MapControl2.AAAMarker.VIA_LOADING;
            errImg = MapControl2.AAAMarker.WAYPOINT_ERROR_MARKER;
        }else if(this._type == MapControl2.AAAMarker.EXPLORATION_MARKER){
            loadingImg = MapControl2.AAAMarker.EXPLORATOR_LOADING;
            errImg = MapControl2.AAAMarker.EXPLORATOR_ERROR;
        }else{
            loadingImg = MapControl2.AAAMarker.LOADING_MARKER;
            errImg = MapControl2.AAAMarker.ERROR_MARKER;
        }
        
        this.setImage(loadingImg);
        this._geoloc.address = '...';
        this._mustBeFormatted = true;
        this._net.performReverseGeocoding(this.getLatLng(), rev_ok, rev_err);
        
        function rev_ok(geoloc){
            me._geoloc.address = geoloc.address;
            me.setImage(myImg);
            me.enableDragging();
            if (checkCallback) checkCallback();
        };
        
        function rev_err(e){
            me._geoloc.address = 'reverse geocoding error!';
            me.setImage(errImg);
            me.enableDragging();
            if (checkCallback) checkCallback();
        };
    },
    // ---------------------------------------------------------------------------------
    
    // ------------- properties --------------------------------------------------------
    getTabs: function()
    {
        return (this._tabs);
    },
    setTabs: function(tabs)
    {
        this._tabs = tabs;
    },
    
    isLocked: function()
    {
      return this._locked;
    },
    setLocked: function(value)
    {
        this._locked = value;
        var lock = this._locked;
        if(this._geoloc.gettingInUsers != null)
            if( this._geoloc.gettingInUsers.length>0 || this._geoloc.gettingOffUsers.length>0 )
                lock = true;
                
        if(lock)
           this.disableDragging();
        else
            this.enableDragging();        
    },
    
    getType: function()
    {
        return this._type;
    },
    
    getGeoLoc: function()
    {
        return (this._geoloc);
    },
    
    setSelectedImage: function(value)
    {
        this._selectedImage = value;
    },
    
    isSelected: function()
    {
        return (this._isShowingInfoWindow);
    },
    
    setType: function(newType){
        this._type = newType;
        this._normalImage = MapControl2.AAAMarker._iconArray[newType];
        this._selectedImage = MapControl2.AAAMarker._selectedIconArray[newType];
        this._currentImage = this._normalImage;
        this.setImage(this._currentImage);
    },
    
    setLatLngFromGeoloc: function(geoloc){
        var ll = MapControl2.AAAUtils.toGLatLng(geoloc);
        this.setLatLng(ll);
        this._geoloc.lat = geoloc.Lat;
        this._geoloc.lng = geoloc.Lng;
        this._geoloc.address = geoloc.Address;
        this._geoloc.description = geoloc.Description;
        this._geoloc.gettingInUsers = geoloc.gettingInUsers;
        this._geoloc.gettingOffUsers = geoloc.gettingOffUsers;
        this.setLocked(this._locked);
        this._mustBeFormatted = true;
    }
    // -----------------------------------------------------------------------------------
};

// ----------------- AAAMarker creation utility ------------------------------------------
// funzione statica di creazione marker
MapControl2.AAAMarker.createMarker = function(point, locked, type, description) 
{
    return (new MapControl2.AAAMarker(point, locked, type, description));
};
// ---------------------------------------------------------------------------------------

// tipi di marker
MapControl2.AAAMarker.START_MARKER = 0;
MapControl2.AAAMarker.END_MARKER = 1;
MapControl2.AAAMarker.STOP_MARKER = 2;
MapControl2.AAAMarker.TAKE_IN_MARKER = 3;
MapControl2.AAAMarker.TAKE_OFF_MARKER = 4;
MapControl2.AAAMarker.WAYPOINT_MARKER = 5;
MapControl2.AAAMarker.ROUTEHANDLER_MARKER = 6;
MapControl2.AAAMarker.EXPLORATION_MARKER = 7;

// icone associate ai vari tipi di marker
MapControl2.AAAMarker._iconArray = new Array(
        "http://www.google.com/mapfiles/dd-start.png",
        "http://www.google.com/mapfiles/dd-end.png",
        "http://www.google.com/mapfiles/dd-pause.png",
        "http://www.google.com/mapfiles/dd-end.png",
        "http://www.google.com/mapfiles/dd-end.png",
        "http://www.google.com/mapfiles/dd-via.png",
        "http://" + window.location.host + "/common/mapfiles/dd-via-selected.png",
        "http://" + window.location.host + "/common/mapfiles/arrow-marker.png"
);

MapControl2.AAAMarker._selectedIconArray = new Array(
        "http://" + window.location.host + "/common/mapfiles/dd-start-selected.png",
        "http://" + window.location.host + "/common/mapfiles/dd-end-selected.png",
        "http://" + window.location.host + "/common/mapfiles/dd-pause-selected.png",
        "http://www.google.com/mapfiles/dd-end.png",
        "http://www.google.com/mapfiles/dd-end.png",
        "http://" + window.location.host + "/common/mapfiles/dd-via-selected.png",
        "http://" + window.location.host + "/common/mapfiles/dd-via-selected.png",
        "http://" + window.location.host + "/common/mapfiles/arrow-marker.png"
        //"http://maps.google.com/mapfiles/kml/pal3/icon61.png"
);

MapControl2.AAAMarker.LOADING_MARKER = "http://" + window.location.host + "/common/mapfiles/dd-loading-marker.png";
MapControl2.AAAMarker.ERROR_MARKER = "http://" + window.location.host + "/common/mapfiles/dd-error-marker.png";
MapControl2.AAAMarker.WAYPOINT_ERROR_MARKER = "http://" + window.location.host + "/common/mapfiles/dd-via-error.png";
MapControl2.AAAMarker.VIA_LOADING = "http://" + window.location.host + "/common/mapfiles/dd-via-loading.png";
MapControl2.AAAMarker.EXPLORATOR_LOADING = "http://" + window.location.host + "/common/mapfiles/arrow-marker-loading.png"
MapControl2.AAAMarker.EXPLORATOR_ERROR = "http://" + window.location.host + "/common/mapfiles/arrow-marker-error.png"

// registrazione classe
MapControl2.AAAMarker.registerClass('MapControl2.AAAMarker', GMarker);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
